﻿namespace EyeOpen.TimeAssistant.Data.IntegrationTest
{
    using System;
    using System.Linq;
    using EyeOpen.Domain;
    using EyeOpen.Security.Server.BootStrapper;
    using EyeOpen.Test.Patch;
    using EyeOpen.TimeAssistant.Domain;
    using EyeOpen.TimeAssistant.Service;
    using EyeOpen.Validation;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using SharpTestsEx;

    /// <summary>
    /// Tests the <see cref="TimeAssistantServiceContext"/>.
    /// </summary>
    [TestClass]
    public class TimeAssistantServiceContextTest : TestBase
    {
        [TestInitialize]
        public void Initialize()
        {
            new TimeAssistantDatabaseFactory().CreateDatabase();
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.TimeAssistant.Service")]
        public void AddAProjectWithPositivePriorityExpectedProjectSaved()
        {
            const byte Priority = 1;
            SaveUserStory(Priority, "Title1", "Description1");
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.TimeAssistant.Service")]
        public void AddAProjectWithZeroPriorityExpectedRightValidationResult()
        {
            const byte Priority = 0;
            const byte ExpectedValidationErrorsNumberPriority = 1;
            SaveUserStory(Priority, "Title1", "Description1", ExpectedValidationErrorsNumberPriority);
        }

        private void SaveUserStory(byte priority, string title, string description, int expectedValidationErrorsNumber = 0)
        {
            var user =
                new User
                    {
                        Id = Guid.NewGuid(),
                    };

            var meta =
                new Meta
                    {
                        Id = Guid.NewGuid(),
                        Title = title,
                        Description = description,
                        User = user
                    };

            var project1 =
                new UserStory
                    {
                        Id = Guid.NewGuid(),
                        Name = "Project1",
                        Priority = priority,
                        Meta = meta,
                        User = user
                    };

            try
            {
                new TimeAssistantServiceContext().
                    SaveUserStory(user, project1);
            }
            catch(ValidationException ex)
            {
                ex
                    .ValidationResults
                    .Count()
                    .Should("Validation results in ValidationException are wrong number.")
                    .Be
                    .EqualTo(expectedValidationErrorsNumber);
            }
        }
    }
}
